<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @Launchform="Launchform3" @findClicked="findData" />

    <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
      <div style="display: flex">
        <div style="width: 70%">
          <Tables
            @modifTable6="modifTable6"
            @modifTable5="modifTable5"
            @modifTable3="modifTable3"
            :isLoading="isLoading"
            :functionList2="true"
            :functionList1="true"
            :tableHeader="tableHeader"
            :tableData="BlankingTableDate"
            :TableHeight="100"
          />
          <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
        </div>

        <div style="margin-left: 30px">
          <Find :searchImg="false" :OriginalFilter="{ static: true }" :formList="formList1" @Launchform="Launchform5" />
          <Table :isLoading="isLoading5" :tableHeader="tableHeader2" :tableData="dateQueryTableDate" :TableHeight="100" />
        </div>
      </div>
    </el-card>

    <el-dialog v-model="dialogVisible6" width="50%" class="dialog pb0">
      <Find @Launchform="Launchform" style="margin-bottom: 20px" :OriginalFilter="{ static: true }" :formList="formList2" />

      <Tables
        @deleteRow="deleteRow"
        :deleteButton="true"
        :isLoading="isLoading1"
        :functionList="true"
        :tableHeader="tableHeader1"
        :tableData="mainQueryTableDate"
        :TableHeight="100"
      />
      <PageModule :PageData="{ total: total1, buttonS: true }" @pageSizeChange="handleSizeChange1" @pageChange="handleCurrentChange1" />
    </el-dialog>

    <el-dialog v-model="dialogVisible7" width="50%" class="dialog pb0">
      <Find @Launchform="Launchform7" style="margin-bottom: 20px" :OriginalFilter="{ static: true }" :formList="formList5" @findClicked="findData1" />
      <div style="display: flex">
        <div style="width: 30%">
          <h1 style="font-size: 18px; margin-bottom: 10px">{{ $t(`Print.FilterTheScreeningSite`) }}</h1>
          <Table
            @selectList="selectList"
            selection
            :isLoading="isLoading7"
            :functionList="true"
            :tableHeader="tableHeader3"
            :tableData="TheCommandOutputTableDate"
            :TableHeight="100"
          />
        </div>

        <div style="margin-left: 50px">
          <el-button type="primary" size="default" @click="BulkReceivingMethods1">{{ $t(`Print.BatchReceiving`) }}</el-button>

          <Table
            @currentRow="clicktogetdata3"
            @selectList="selectList1"
            selection
            :isLoading="isLoading8"
            :functionList="true"
            :tableHeader="tableHeader5"
            :tableData="Blanking1TableDate"
            :TableHeight="100"
          />
          <PageModule :PageData="{ total: total5, buttonS: true }" @pageSizeChange="handleSizeChange5" @pageChange="handleCurrentChange5" />
        </div>
      </div>
    </el-dialog>

    <el-dialog v-model="dialogVisible8" width="50%" class="dialog pb0">
      <Find @Launchform="Launchform7" style="margin-bottom: 20px" :OriginalFilter="{ static: true }" :formList="formList5" />
      <div style="display: flex">
        <div style="width: 30%">
          <h1 style="font-size: 18px; margin-bottom: 10px">{{ $t(`Print.FilterTheScreeningSite`) }}</h1>
          <Table
            @selectList="selectList"
            selection
            :isLoading="isLoading7"
            :functionList="true"
            :tableHeader="tableHeader3"
            :tableData="TheCommandOutputTableDate"
            :TableHeight="100"
          />
        </div>

        <div style="margin-left: 50px">
          <el-button type="primary" size="default" @click="BulkReceivingMethods2">{{ $t(`Print.BatchIssuance`) }}</el-button>

          <Table
            @currentRow="clicktogetdata3"
            @selectList="selectList1"
            selection
            :isLoading="isLoading8"
            :functionList="true"
            :tableHeader="tableHeader5"
            :tableData="Blanking1TableDate"
            :TableHeight="100"
          />
          <PageModule :PageData="{ total: total3, buttonS: true }" @pageSizeChange="handleSizeChange2" @pageChange="handleCurrentChange2" />
        </div>
      </div>
    </el-dialog>

    <el-dialog v-model="dialogVisible9" width="50%" class="dialog pb0">
      <Table
        @selectList="selectList5"
        selection
        :isLoading="isLoading8"
        :functionList="true"
        :tableHeader="tableHeader6"
        :tableData="BatchQueryMethodsTableDate"
        :TableHeight="100"
      />
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'ProcessTransceiver'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import {
  BatchQuery,
  mainQuery,
  Blanking,
  TransceiverType,
  DetailDeletion,
  dateQuery,
  TheCommandOutput,
  Blanking1,
  BulkReceiving
} from '@/api/WarehouseDepartment/WebPages/ProcessTransceiver'
import { ElMessage, ElMessageBox } from 'element-plus'

const BatchQueryMethodsTableDate = ref([])

const batchNoListList = ref([])
const selectList5 = (row: any) => {
  batchNoListList.value = row.map((item: any) => {
    return item.batchNo
  })
}

// 批次查询
const BatchQueryParms = ref({
  lotNo: '',
  lsNo: '',
  partName: '',
  sizeNo: ''
})

const clicktogetdata3 = (row: any, list: any) => {
  BatchQueryParms.value.lsNo = row.lsNo
  BatchQueryParms.value.partName = row.partName
  BatchQueryParms.value.sizeNo = row.sizeNo
  BatchQueryMethods()
  dialogVisible9.value = true
}

const dialogVisible9 = ref(false)

const BatchQueryMethods = async () => {
  try {
    const result = await BatchQuery(BatchQueryParms.value)
    BatchQueryMethodsTableDate.value = result
  } catch (error) {
    JMessage.error('批次查询失败请重视')
  }
}

// 发料
const dialogVisible8 = ref(false)

const modifTable6 = (row: any) => {
  Blanking1Parms.value.lotNo = row.row.lotNo
  Blanking1Parms.value.type = 0
  TheCommandOutputParms.value.lotNo = row.row.lotNo
  TheCommandOutputParms.value.type = 0
  BatchQueryParms.value.lotNo = row.row.lotNo

  TheCommandOutputMethods()
  // Blanking1Methods()
  dialogVisible8.value = true
}

const checkboxChange1 = (row: any) => {
  if (row.inQty < row.inQty1) {
    JMessage.warning('报数不可大于剩余数量')
    row.inQty1 = 0
  }
}

// 收料
const BulkReceivingParms = ref<any>([])

const selectList1 = (row: any) => {
  BulkReceivingParms.value = row.map((item: any) => {
    item.lotNo = Blanking1Parms.value.lotNo
    item.collectPad = 'web'
    return item
  })
}

const selectList = (row: any) => {
  Blanking1Parms.value.partNames = row.map((item: any) => {
    return item.partName
  })
  if (Blanking1Parms.value.partNames.length !== 0) {
    Blanking1Methods()
  } else {
    Blanking1Parms.value.partNames = null
    Blanking1Methods()
  }
}

const BulkReceivingMethods1 = () => {
  Avtive.value = 'H'
  BulkReceivingMethods()
}

const BulkReceivingMethods2 = () => {
  Avtive.value = 'S'
  BulkReceivingMethods()
}

const Avtive = ref('')

const BulkReceivingMethods = async () => {
  if (BulkReceivingParms.value.length === 0) {
    JMessage.warning('请勾选需要批量收料的数据')
  } else {
    BulkReceivingParms.value = BulkReceivingParms.value.map((item: any) => {
      item.type = Avtive.value
      item.inQty = item.inQty1
      return item
    })
    try {
      const result = await BulkReceiving(BulkReceivingParms.value)
      if (result.code === 200) {
        Blanking1Methods()
        JMessage.success(result.message)
      } else {
        JMessage.error(result.message)
      }
    } catch (error) {
      JMessage.error('批量收料出错请重试')
    }
  }
}

// 表格回显1
const Blanking1TableDate = ref([])

const total3 = ref(0)

const handleSizeChange2 = (val: number) => {
  Blanking1Parms.value.pageSize = val
  Blanking1Methods()
}

const handleCurrentChange2 = (val: number) => {
  Blanking1Parms.value.pageNum = val
  Blanking1Methods()
}

const Launchform7 = (row: any) => {
  Blanking1Parms.value.lsNos = row[1].selectValue1
  Blanking1Parms.value.sizeNos = row[0].selectValue1
  Blanking1Methods()
}

const Blanking1Parms = ref<any>({
  lotNo: '',
  lsNos: null,
  partNames: null,
  sizeNos: null,
  type: '',
  pageNum: 1,
  pageSize: 40
})

const isLoading8 = ref(false)

const total5 = ref(0)

const handleSizeChange5 = (val: number) => {
  Blanking1Parms.value.pageSize = val
  Blanking1Methods()
}

const handleCurrentChange5 = (val: number) => {
  Blanking1Parms.value.pageNum = val
  Blanking1Methods()
}

const Blanking1Methods = async () => {
  isLoading8.value = true
  try {
    const result = await Blanking1(Blanking1Parms.value)
    Blanking1TableDate.value = result.dtos.map((item: any) => {
      item.inQty1 = item.inQty
      // item.change = 'Y'
      // item.disableConfirm = true
      item.partName1 = item.partName1.value
      item.partName = item.partName1.key
      return item
    })
    isLoading8.value = false
    total5.value = result.total
    total3.value = result.total
  } catch (error) {
    isLoading8.value = false
    JMessage.error('表格查询出错')
  }
}

// 收发料左面表格数据
const TheCommandOutputParms = ref<any>({
  lotNo: '',
  lsNos: null,
  partNames: null,
  sizeNos: null,
  type: ''
})

const isLoading7 = ref(false)

const TheCommandOutputTableDate = ref<any>([])

const TheCommandOutputMethods = async () => {
  isLoading7.value = true
  try {
    const result = await TheCommandOutput(TheCommandOutputParms.value)

    TheCommandOutputTableDate.value = result.partName1.map((item: any) => {
      return { partName1: item.partName1.value, partName : item.partName1.key }
    })

    console.log(result)

    formList5.value[0].selectOptions = EvaluatesTheArrayReturnValue(result.sizeNo)
    formList5.value[1].selectOptions = EvaluatesTheArrayReturnValue(result.lsNo)

    isLoading7.value = false
  } catch (error) {
    isLoading7.value = false
    JMessage.error('部位数据查询出错')
  }
}

console.log(TheCommandOutputTableDate.value)

const dialogVisible7 = ref(false)

const modifTable5 = (row: any) => {
  TheCommandOutputParms.value.lotNo = row.row.lotNo
  Blanking1Parms.value.lotNo = row.row.lotNo
  Blanking1Parms.value.type = 1
  TheCommandOutputParms.value.lotNo = row.row.lotNo
  TheCommandOutputParms.value.type = 1
  BatchQueryParms.value.lotNo = row.row.lotNo

  TheCommandOutputMethods()
  // Blanking1Methods()
  dialogVisible7.value = true
}

// 日期查询
const dateQueryParms = ref({
  dateInfo: ''
})

const Launchform5 = (row: any) => {
  dateQueryParms.value.dateInfo = row[0].dateValue
  dateQueryMethods()
}

const dateQueryTableDate = ref<any>([])

const isLoading5 = ref(false)

const dateQueryMethods = async () => {
  isLoading5.value = true
  try {
    let obj: any = {}
    let obj1: any = {}
    let obj2: any = {}
    let obj4: any = {}

    const result = await dateQuery(dateQueryParms.value.dateInfo)

    for (const key in result) {
      if (key.includes('Lot')) {
        obj[key] = result[key]
      }

      if (key.includes('Match')) {
        obj4.inLotNum = result.inMatchNum
        obj4.outLotNum = result.outMatchNum
      }

      if (key.includes('Part')) {
        obj1.inLotNum = result.inPartNum
        obj1.outLotNum = result.outPartNum
      }

      if (key.includes('Bag')) {
        obj2.inLotNum = result.inBagNum
        obj2.outLotNum = result.outBagNum
      }
    }

    dateQueryTableDate.value = [obj, obj1, obj2, obj4]
    dateQueryTableDate.value[0].title = '指令个数'
    dateQueryTableDate.value[1].title = '部件个数'
    dateQueryTableDate.value[2].title = '袋数'
    dateQueryTableDate.value[3].title = '当日配套数'
    isLoading5.value = false
  } catch (error) {
    JMessage.error('日期查询出错')
    isLoading5.value = false
  }
}

// 删除
const DetailDeletionParms = ref()

const deleteRow = (row: any) => {
  DetailDeletionParms.value = row.row
  handleCloseMateriaLKlc()
}

const handleCloseMateriaLKlc = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗?')
    .then(async () => {
      DetailDeletionMethods()
    })
    .catch(() => {})
}

const DetailDeletionMethods = async () => {
  isLoading1.value = true
  DetailDeletionParms.value.lotNo = mainQueryParms.value.lotNo
  try {
    const result = await DetailDeletion(DetailDeletionParms.value)
    if (result.code === 200) {
      mainQueryMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('删除出错请重试')
  }
  isLoading1.value = false
}

// 收发类型
const TransceiverTypeMethods = async () => {
  try {
    const result = await TransceiverType()
    formList2.value[0].selectOptions = EvaluatesTheArrayReturnValue(result)
  } catch (error) {
    JMessage.error('收发类型明细查询出错')
  }
}

const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

// 冲裁查询
const BlankingParms = ref({
  lotNo: '',
  pageNum: 1,
  pageSize: 40,
  style: '',
  switchTimeEnd: '',
  switchTimeStart: '',
  type: ''
})

const isLoading = ref(true)

const BlankingTableDate = ref([])

const BlankingMethods = async () => {
  isLoading.value = true

  try {
    const result = await Blanking(BlankingParms.value)
    BlankingTableDate.value = result.dtos
    total.value = result.total
    isLoading.value = false
  } catch (error) {
    JMessage.warning('查询异常')
    isLoading.value = false
  }
}

const Launchform3 = (row: any) => {
  BlankingParms.value.lotNo = row[0].inputValue
  BlankingParms.value.style = row[1].inputValue
  BlankingParms.value.switchTimeStart = row[2].dateValue[0]
  BlankingParms.value.switchTimeEnd = row[2].dateValue[1]
  BlankingMethods()
}

const total = ref(0)

const handleSizeChange = (val: number) => {
  BlankingParms.value.pageSize = val
  BlankingMethods()
}

const handleCurrentChange = (val: number) => {
  BlankingParms.value.pageNum = val
  BlankingMethods()
}

onMounted(() => {
  BlankingMethods()
})

const formList1 = ref([
  {
    type: 'dateSingle',
    dateLabel: '日期',
    dateValue: ''
  }
])

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '型体',
    inputValue: ''
  },

  {
    type: 'date',
    dateLabel: '针车上线日',
    dateValue: ''
  },

  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const findData = (row: any) => {
  if (row === '置空') {
    formList.value.forEach((item: any) => {
      item.dateValue = ''
    })
  }
}

// 冲裁查询
const mainQueryParms = ref({
  lotNo: '',
  lsCollectType: '',
  pageNum: 1,
  pageSize: 40,
  partName: '',
  sizeNo: ''
})

const dialogVisible6 = ref(false)

const Launchform = (row: any) => {
  mainQueryParms.value.lsCollectType = row[0].selectValue
  mainQueryParms.value.partName = row[1].inputValue
  mainQueryParms.value.sizeNo = row[2].inputValue

  mainQueryMethods()
}

const total1 = ref(0)

const handleSizeChange1 = (val: number) => {
  mainQueryParms.value.pageSize = val
  mainQueryMethods()
}

const handleCurrentChange1 = (val: number) => {
  mainQueryParms.value.pageNum = val
  mainQueryMethods()
}

// 收发明细
const modifTable3 = (row: any) => {
  console.log(row)
  mainQueryParms.value.lotNo = row.lotNo
  dialogVisible6.value = true
  Blanking1Parms.value.lotNo = row.lotNo
  mainQueryMethods()
  TransceiverTypeMethods()
  BatchQueryParms.value.lotNo = row.lotNo
}

const formList2 = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '收发类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '部位',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '号码',
    inputValue: ''
  }
])

const formList5 = ref([
  {
    type: 'select1',
    selectValue: '',
    selectP: '号码',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select1',
    selectValue: '',
    selectP: '轮次',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const findData1 = (row: string) => {
  if (row === '置空') {
    formList5.value.forEach((item: any) => {
      item.selectValue1 = null
    })
  }
}

const mainQueryTableDate = ref([])

const isLoading1 = ref(false)

const mainQueryMethods = async () => {
  isLoading1.value = true
  try {
    const result = await mainQuery(mainQueryParms.value)
    mainQueryTableDate.value = result.dtos.map((item:any) => {
      item.partName1 = item.partName1.value
      item.partName = item.partName1.key
      return item
    })
    total1.value = result.total
    isLoading1.value = false
  } catch (error) {
    isLoading1.value = false
    JMessage.error('查询出错')
  }
}
// 表格数据
const tableHeader = ref([
  {
    label: '指令号',
    prop: 'lotNo'
  },
  {
    label: '型体',
    prop: 'style'
  },
  {
    label: '颜色',
    prop: 'color'
  },
  {
    label: '订单量',
    prop: 'orderQty'
  },
  {
    label: '累计入库',
    prop: 'prodQty'
  },
  {
    label: '入库欠数',
    prop: 'inShort'
  },
  {
    label: '累计出库',
    prop: 'sendQty'
  },
  {
    label: '出库欠数',
    prop: 'outShort'
  }
])

const tableHeader1 = ref([
  {
    label: '类型',
    prop: 'lsCollectType'
  },
  {
    label: '操作时间',
    prop: 'collectTime'
  },
  {
    label: '设备编号',
    prop: 'collectPad'
  },
  {
    label: '部位',
    prop: 'partName1'
  },
  {
    label: '轮次',
    prop: 'lsNo'
  },
  {
    label: '号码',
    prop: 'sizeNo'
  },
  {
    label: '标签',
    prop: 'labelNo'
  },
  {
    label: '数量',
    prop: 'inQty'
  }
])

const tableHeader2 = ref([
  {
    label: '-',
    prop: 'title'
  },
  {
    label: '收料',
    prop: 'inLotNum'
  },
  {
    label: '发料',
    prop: 'outLotNum'
  }
])

const tableHeader3 = ref([
  {
    label: '类型',
    prop: 'partName1'
  }
])

const tableHeader5 = ref([
  {
    label: '部位',
    prop: 'partName1',
    change: true
  },
  {
    label: '号码',
    prop: 'sizeNo',
    change: true
  },
  {
    label: '轮次',
    prop: 'lsNo',
    change: true
  },
  {
    label: '剩余数量',
    prop: 'inQty',
    change: true
  },
  {
    label: '报数',
    prop: 'inQty1'
  }
])

const tableHeader6 = ref([
  {
    label: '批次',
    prop: 'batchNo',
    change: true
  },
  {
    label: '最少收/发数量',
    prop: 'qty',
    change: true
  }
])
</script>

<style lang="less" scoped></style>
